package com.pussinboots.morning.cms.modules.goods.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.pussinboots.morning.cms.common.security.AuthorizingUser;
import com.pussinboots.morning.cms.common.util.ResultBean;
import com.pussinboots.morning.cms.common.util.SingletonLoginUtils;
import com.pussinboots.morning.cms.modules.system.product.entity.OsProductParameter;
import com.pussinboots.morning.cms.modules.system.product.entity.OsProductSpecification;
import com.pussinboots.morning.cms.modules.system.product.service.IOsProductSpecificationService;
import com.pussinboots.morning.common.controller.BaseController;

/**
 * 
* 项目名称：morning-cms-webapp   
* 类名称：SystemVersionLogControlller   
* 类描述：版本日至表示层控制器    
* 创建人：陈星星   
* 创建时间：2017年2月17日 上午12:32:07
 */
@Controller
@RequestMapping(value = "/system/goods")
public class GoodsListSpecificationControlller extends BaseController {
	
	
	/** 管理员列表 */
	private static final String SYSTEM_GOODS_LIST_SET_SPECIFICATION = getViewPath("modules/goods/system_goods_list_set_specification");
	

	@Autowired
	private IOsProductSpecificationService productSpecificationService;
	
	
	/**
	 * GET 跳转道添加商品页面
	 * @return
	 */
	//@RequiresPermissions("system:goods:list:add")
	@GetMapping(value = "/list/add/setSpecification/{id}")
	public String tosetSpecification(Model model,@PathVariable("id") Long productId) {
		model.addAttribute("productId", productId);
		return SYSTEM_GOODS_LIST_SET_SPECIFICATION;
	}
	
	/**
	 * GET 跳转道添加商品页面
	 * @return
	 */
	//@RequiresPermissions("system:goods:list:add")
	@PostMapping(value = "/list/add/setSpecification",produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public ResultBean setSpecification(Model model, @RequestBody JSONObject jsonParam) {
		
		ResultBean result = new ResultBean();
		
		String userName = null;
		Long productId = null;
		AuthorizingUser authorizingUser = SingletonLoginUtils.getUser();
		if(authorizingUser!=null){
			userName = authorizingUser.getLoginName();
		}else{
			result.setCode(0);
			result.setSuccess(false);
			result.setMessage("未登录或登陆超时，请重新登陆");
		}
		
		
		if(!jsonParam.isEmpty()){
			Date createTime = new Date();
			OsProductSpecification entity = jsonParam.toJavaObject(OsProductSpecification.class);
			
			entity.setCreateBy(userName);
			entity.setCreateTime(createTime);
			entity.setUpdateBy(userName);
			entity.setUpdateTime(createTime);
			
			productSpecificationService.insert(entity);
			
			Wrapper<OsProductParameter> wrapper = new EntityWrapper<>();
			wrapper.where("product_id={0}", productId);
			
			
			result.setCode(1);
			result.setSuccess(true);
			result.setMessage("添加成功");
		}else{
			result.setCode(0);
			result.setSuccess(false);
			result.setMessage("关联分类错误，未正确获取分类编号");
		}
		
		return result;
	}
	

	
}
